#!/usr/bin/env bash

#VERSION=`git describe --tags`
VERSION=`sed -n '2s!.*version=\"\([0-9\.a-z-]*\)\".*!\1!p' config.xml`
echo "上一次的版本号为$VERSION"
read -p "请输入新版本号: "

if [ -z "$REPLY" ]; then
	echo "将使用上一次的版本号$VERSION"
else
	VERSION=$REPLY
	ANDROID_VERSION=30000201
	# 因为Mac下的sed命令和Linux下的sed命令不同，根据系统实际情况来调用sed
	if [ "$(uname)" == "Darwin" ]; then
		sed -i "" "2s!\(version=\"\)[0-9\.a-z-]*\"!\1$VERSION\"!" config.xml
		sed -i "" "2s!\(android-versionCode=\"\)[0-9\.a-z-]*\"!\1$ANDROID_VERSION\"!" config.xml
		sed -i "" "s!\(constant('appVer', '\)[0-9\.a-z-]*'!\1$VERSION'!" app/index.html
		sed -i "" "s!\(constant('appVer', '\)[0-9\.a-z-]*'!\1$VERSION'!" app/test_index.html
		sed -i "" "s!\(constant('appVer', '\)[0-9\.a-z-]*'!\1$VERSION'!" app/real_index.html
		sed -i "" "s!\(constant('appVer', '\)[0-9\.a-z-]*'!\1$VERSION'!" app/app_index.html
        sed -i "" "s!\(constant('appVer', '\)[0-9\.a-z-]*'!\1$VERSION'!" app/test_app_index.html
	else # TODO 待测试，在linux下sed更改版本号
		sed -i "2s!\(version=\"\)[0-9\.a-z-]*\"!\1$VERSION\"!" config.xml
		sed -i "s!\(constant('appVer', '\)[0-9\.a-z-]*'!\1$VERSION'!" app/index.html
		sed -i "" "s!\(constant('appVer', '\)[0-9\.a-z-]*'!\1$VERSION'!" app/test_index.html
		sed -i "" "s!\(constant('appVer', '\)[0-9\.a-z-]*'!\1$VERSION'!" app/app_index.html
	fi
	echo "已更新版本号为$VERSION"
fi
